﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;
using System.Text;

public partial class adm_assignusercat : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
    }

    private void _BindDataGrid()
    {

    }

    private void _BindMaincat()
    {
        if (this.drpChooseType.SelectedIndex == 0)
        {
            int i=0;
            this.drpMainCat.Items.Add("Select one");
            this.drpMainCat.Items[i].Value="0";
            foreach(a_CatMain ac in a_CatMainService.Geta_CatMainList(a_CatMainColumns.UName, OrderDirection.ASC))
            {
                i++;
                this.drpMainCat.Items.Add(ac.UName);
                this.drpMainCat.Items[i].Value=ac.CatID.ToString();
            }
        }
        else
        {
            this._BindSubcat();
        }
    }

    private void _BindSubcat()
    {
        if (this.drpMainCat.Items.Count > 0)
        {
            this.drpSubCat.DataSource = a_CatSubService.Geta_CatSubListByCatMain(int.Parse(this.drpMainCat.SelectedValue));
            this.drpSubCat.DataBind();
        }
        else
        {
            this.drpSubCat.DataSource = a_CatSubService.Geta_CatSubList(a_CatSubColumns.UName, OrderDirection.ASC);
            this.drpSubCat.DataBind();
        }
        this._BindCategory();
    }

    private void _BindDropdownList()
    {
        this.drpSelectUserroles.Items.Clear();
        this.drpSelectUserroles.Items.Add("Select One");
        this.drpSelectUserroles.Items[0].Value = "0";
        this.drpSelectUserroles.Items.Add("Approver");
        this.drpSelectUserroles.Items[1].Value = "2";
        this.drpSelectUserroles.Items.Add("Editor");
        this.drpSelectUserroles.Items[2].Value = "3";
    }

    private void _AddtoCategory()
    {
        foreach (RepeaterItem ri in this.rptFullName.Items)
        {
            int uid = 0;
            CheckBox chk = (CheckBox)ri.FindControl("chkName");
            Label lblUserId = (Label)ri.FindControl("lblUID");
            if (chk != null && chk.Checked)
            {
                byte catlevel = byte.Parse(this.drpChooseType.SelectedValue);
                a_UserAccessCategory auc;
                //Kiemm tra ton tai
                a_UserAccessCategoryCollection aucs = a_UserAccessCategoryService.Geta_UserAccessCategoryList_cid_uid(int.Parse(lblUserId.Text), int.Parse(this.drpMainCat.SelectedValue), a_UserAccessCategoryColumns.AID, OrderDirection.ASC);
                if (aucs.Count > 0)
                {
                    auc = aucs[0];
                    a_UserAccessCategoryService.Deletea_UserAccessCategory(auc.AID);
                }
                auc = new a_UserAccessCategory();
                if (this.drpChooseType.SelectedIndex == 0)
                {
                    auc.CatID = int.Parse(this.drpMainCat.SelectedValue);
                }
                else
                {
                    auc.CatID = int.Parse(this.drpSubCat.SelectedValue);
                }
                auc.CatLevel = catlevel;
                auc.UserID = int.Parse(lblUserId.Text);

                a_UserAccessCategoryService.Createa_UserAccessCategory(auc);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Add user to category sucessfully");
                this._BindCategory();
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "You must choose one category");
            }
        }
    }

    private void _RemovetoCategory()
    {
        try
        {
            foreach (RepeaterItem ri in this.rptUserRoles.Items)
            {
                CheckBox chk = (CheckBox)ri.FindControl("chckRole");
                if (chk != null && chk.Checked)
                {
                    int cid = int.Parse(((Label)ri.FindControl("lblCatID")).Text);
                    int uid = int.Parse(((Label)ri.FindControl("lblUserID")).Text);

                    a_UserAccessCategoryService.Deletea_UserAccessCategory(a_UserAccessCategoryService.Geta_UserAccessCategoryList_cid_uid(uid, cid, a_UserAccessCategoryColumns.AID, OrderDirection.ASC)[0].AID);

                    CommonFunc.ShowAlertMessage(this.lblAlert, "Delete sucessfully");
                }
            }
            this._BindCategory();
        }
        catch (Exception ex)
        {
            //throw exceitpon
        }
    }

    public string GetUserName(int uid)
    {
        try
        {
            a_User au = a_UserService.Geta_User(uid);
            return au.Lastname + " " + au.Firstname;
        }
        catch (Exception ex)
        {
            //throw exception
        }
        return "";
    }

    public string GetCategoryname(int cid, byte clevel)
    {
        try
        {
            if (clevel == 1)
            {
                a_CatMain acm = a_CatMainService.Geta_CatMain(cid);
                if (acm != null)
                {
                    return acm.UName;
                }
                else
                {
                    return "";
                }
            }
            else
            {
                a_CatSub acs = a_CatSubService.Geta_CatSub(cid);
                if (acs != null)
                {
                    return acs.Name;
                }
                else
                {
                    return "";
                }
            }
        }
        catch (Exception ex)
        {
            //throw exception
            return "";
        }
    }

    public string GetUserType(int uid)
    {
        try
        {
            a_User au = a_UserService.Geta_User(uid);
            if (au != null)
            {
                return a_UserTypeService.Geta_UserType(au.UserTypeID).Name;
            }
            else
            {
                return "";
            }
        }
        catch (Exception ex)
        {
            //throw exception
            return "";
        }
    }


    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._BindDropdownList();
            this._BindMaincat();
        }
    }

    private void _BindCategory()
    {
        int cid = 0;
        if (this.drpChooseType.SelectedValue == "1")
        {
            cid = int.Parse(this.drpMainCat.SelectedValue);
        }
        else
        {
            cid = int.Parse(this.drpSubCat.SelectedValue);
        }
        if (cid != 0)
        {
            this.rptUserRoles.DataSource = a_UserAccessCategoryService.Geta_UserAccessCategoryList_cid(cid, a_UserAccessCategoryColumns.AID, OrderDirection.ASC);
            this.rptUserRoles.DataBind();
        }
        else
        {
            a_UserAccessCategoryCollection arr=new a_UserAccessCategoryCollection();
            foreach (RepeaterItem ri in this.rptFullName.Items)
            {
                int uid = 0;
                CheckBox chk = (CheckBox)ri.FindControl("chkName");
                Label lblUserId = (Label)ri.FindControl("lblUID");
                if (chk != null && chk.Checked)
                {
                    byte catlevel = byte.Parse(this.drpChooseType.SelectedValue);
                    uid = int.Parse(lblUserId.Text);
                    
                    foreach(a_UserAccessCategory auac in a_UserAccessCategoryService.Geta_UserAccessCategoryList_uid(uid,a_UserAccessCategoryColumns.AID,OrderDirection.ASC))
                    {
                        arr.Add(auac);
                    }
                }
                else
                {
                    CommonFunc.ShowAlertMessage(this.lblAlert, "You must choose one category");
                }
            }
            this.rptUserRoles.DataSource=arr;
            this.rptUserRoles.DataBind();
        }
    }

    private void _BindUsers()
    {
        this.rptFullName.DataSource = a_UserService.Geta_UserList_typeid(int.Parse(this.drpSelectUserroles.SelectedValue), a_UserColumns.Firstname, OrderDirection.ASC);
        this.rptFullName.DataBind();
    }

    protected void btnCategoryGo_Click(object sender, EventArgs e)
    {
        this._BindCategory();
    }

    protected void btnListUserGo_Click(object sender, EventArgs e)
    {
        this.pnlUserRole.Visible = false;
        this._BindUsers();
    }

    protected void drpChooseType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpChooseType.SelectedIndex == 0)
        {
            this.lblMaincat.CssClass = "change";
            this.lblSubcat.CssClass = "clsTitle";
            this._BindDropdownList();
        }
        else
        {
            this.lblMaincat.CssClass = "clsTitle";
            this.lblSubcat.CssClass = "change";
            this._BindDropdownList();
        }
    }

    protected void drpMainCat_SelectedIndexChanged(object sender, EventArgs e)
    {
        //this._BindDropdownList();
        //this._BindMaincat();
        this._BindSubcat();
    }

    protected void btnRemoveFromCategory2_Click(object sender, EventArgs e)
    {
        this._RemovetoCategory();
    }

    protected void btnRemoveFromCategory1_Click(object sender, EventArgs e)
    {
        this._RemovetoCategory();
    }

    protected void btnAddToCategory1_Click(object sender, EventArgs e)
    {
        if (this.rptFullName.Items.Count > 0)
        {
            this._AddtoCategory();
            this.pnlUserRole.Visible = true;
        }
        else
        {
            CommonFunc.ShowAlertMessage(this.lblAlert, "You must choose user to add");
        }
    }

    protected void btnAddCategory2_Click(object sender, EventArgs e)
    {
        this._AddtoCategory();
    }
    #endregion 
    protected void btnViewRoles_Click(object sender, EventArgs e)
    {
        this.pnlUserRole.Visible = true;
        this._BindCategory();
    }
}
